﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace WPFNote.Utilities.Http
{
    /// <summary>
    ///  IP操作
    /// </summary>
    public class IpHelper
    {
        private readonly IHttpContextAccessor _httpContextAccessor;


        public IpHelper(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// IP地址
        /// </summary>
        public string Ip => GetIp();

        /// <summary>
        /// 获取客户端Ip
        /// </summary>
        public string ClientUserIp => GetClientUserIp();

        /// <summary>
        /// 获取服务器内网IP
        /// </summary>
        public string ServerIntranetIp => GetServerIntranetIp();

        /// <summary>
        /// 获取Host
        /// </summary>
        public string Host => GetHost();

        /// <summary>
        /// 获取端口
        /// </summary>
        public int? Port => GetPort();

        /// <summary>
        /// 获取Host（带端口）
        /// </summary>
        public string HostWithPort => GetHostWithPort();

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        private string GetIp()
        {
            return _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString();
        }

        /// <summary>
        /// 获取客户端Ip
        /// </summary>
        /// <returns></returns>
        private string GetClientUserIp()
        {
            var ip = _httpContextAccessor.HttpContext?.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString();
            }
            return ip;
        }

        /// <summary>
        /// 获取服务器内网IP
        /// </summary>
        /// <returns></returns>
        private string GetServerIntranetIp()
        {
            return NetworkInterface
                .GetAllNetworkInterfaces()
                .Select(p => p.GetIPProperties())
                .SelectMany(p => p.UnicastAddresses)
                .FirstOrDefault(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address))?.Address.ToString();
        }


        /// <summary>
        /// 获取Host
        /// </summary>
        /// <returns></returns>
        private string GetHost()
        {
            return _httpContextAccessor.HttpContext?.Request.Host.HasValue == true
                ? _httpContextAccessor.HttpContext?.Request.Host.Host
                : "";
        }

        /// <summary>
        /// 获取Port
        /// </summary>
        /// <returns></returns>
        private int? GetPort()
        {
            return _httpContextAccessor.HttpContext?.Request.Host.HasValue == true
                ? _httpContextAccessor.HttpContext?.Request.Host.Port
                : null;
        }

        /// <summary>
        /// 获取Host（带端口）
        /// </summary>
        /// <returns></returns>
        private string GetHostWithPort()
        {
            return _httpContextAccessor.HttpContext?.Request.Host.HasValue == true
                ? _httpContextAccessor.HttpContext?.Request.Host.Value
                : "";
        }
    }
}
